/*-----------------------------------------------------------------
 * Econ	functions towards production
 *
 */

#include "copyrite.h"
#include "config.h"
#include <string.h>
#include <strings.h>
#include <math.h>
#include "conf.h"
#include "externs.h"
#include "parse.h"
#include "confmagic.h"
#include "function.h"
#include "intrface.h"
#include "ansi.h"
#include "match.h"
#include "space.h"
#include "flags.h"
#include "dbdefs.h"
#include "mushdb.h"
#include "lock.h"
#include "dbdefs.h"
#include "attrib.h"
#include "log.h"
#include "econ.h"

int do_trigger_food(dbref enactor, dbref zone) {
	ATTR *a;
	char req_stuff[BUFFER_LEN];
	char *rp;
	char tmp[BUFFER_LEN];
	char *tp;
	char local_currency[10];
	char food_sour[10];
	char food_dest[10];
	char *fs;
	char *fd;
	
	char *commod_needed;
	double qty_needed, life_income, investment;
	
	struct bank_t bankd;
	struct commods_t commod;
	
	double res_level, tech_level;
	double total_space, usedspace;
	double morale;
	double population;
	double agri_workers;
	double can_make;
	double rent, total_rent, total_agri_wages;
	double slaves, wage_agr, health, health_inc;
	
	double temp_d, temp_d2, tmpd;	
	int no_of_reqs, err;
	struct commods_t com_info;
	
	fs = &food_sour[0];
	fd = &food_dest[0];
	tp = &tmp[0];
		
	// Get requries...		
	rp = &req_stuff[0];
	a = atr_get(zone, HOURLY_REQ_ATTR);
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing %s attribute.",HOURLY_REQ_ATTR), 0);		
		return 0;
	}	
	strcpy(req_stuff, atr_value(a));	
	
	
	// Calculate the research level...
	a = atr_get(zone, RESEARCH_ATTR);
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing %s attribute.",RESEARCH_ATTR), 0);		
		return 0;
	}
	tech_level = parse_double(atr_value(a));	
	res_level = tech_level + (tech_level * 0.01);
	
	// Get population...
	a = atr_get(zone, POPULATION_ATTR);
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing %s attribute.",POPULATION_ATTR), 0);		
		return 0;
	}
	population = parse_double(atr_value(a));	
	
	// Get agri_workers...
	a = atr_get(zone, AGRI_WORKERS_ATTR);
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing %s attribute.",AGRI_WORKERS_ATTR), 0);
		return 0;
	}
	agri_workers = parse_double(atr_value(a));
	
	a = atr_get(zone, SLAVES_ATTR);
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing %s attribute.",SLAVES_ATTR), 0);
		return 0;
	}
	slaves = parse_double(atr_value(a));
		
	a = atr_get(econ_parent, "WAGE_AGR");
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing WAGE_AGR attribute."), 0);
		return 0;
	}
	wage_agr = parse_double(atr_value(a));
	
	a = atr_get(zone, "LOCAL_CURRENCY");
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing LOCAL_CURRENCY attribute."), 0);
		return 0;
	}
	strcpy(local_currency, atr_value(a));
		
	a = atr_get(econ_parent, RENT_ATTR);
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing %s attribute.", RENT_ATTR), 0);
		return 0;
	}
	strcpy(tmp, atr_value(a));
	exch(local_currency, tmp);
	rent = parse_double(tmp);
	
	a = atr_get(econ_parent, "HEALTH_INC");
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing HEALTH_INC attribute."), 0);
		return 0;
	}
	health_inc = parse_double(atr_value(a));
	
	a = atr_get(zone, "HEALTH");
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing HEALTH attribute."), 0);
		return 0;
	}
	health = parse_double(atr_value(a));
	
	a = atr_get(zone, "LIFE_INCOME");
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing LIFE_INCOME attribute."), 0);
		return 0;
	}
	life_income = parse_double(atr_value(a));
	
	a = atr_get(zone, "INVESTMENT");
	if (a == NULL) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("Halted due to missing INVESTMENT attribute."), 0);
		return 0;
	}
	investment = parse_double(atr_value(a));
	
	// Init some values...
	morale = get_morale(zone);
	morale = morale * 0.01;
	no_of_reqs = counts(req_stuff, '|');
		
	// Define the temp storages...		
	commod_needed = (char *)malloc(sizeof(char) * BUFFER_LEN);
	qty_needed = 0;
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("Food Gathering Cycle started on %s.",unparse_dbref(zone)), 0);
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  %i hourly requirements.",ECON_OK, no_of_reqs), 0);
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  %f agri workers.",ECON_OK, agri_workers), 0);
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The morale is %f.",ECON_OK, morale), 0);
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The techlevel is %f",ECON_OK, res_level), 0);
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The population is %f",ECON_OK, population), 0);
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The number of slaves is %f",ECON_OK, slaves), 0);
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The rental cost is %f",ECON_OK, rent), 0);
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The local currency is %s",ECON_OK, local_currency), 0);
	
	(void) get_food_source(zone, fs, fd);
	
	do {
		if (strlen(rp) == 0) break;
		strcpy(commod_needed, strsep(&rp, "|"));
		if (commod_needed != NULL) {
			qty_needed = parse_double(strsep(&rp, " "));
			qty_needed = (population / 500) * qty_needed;
		} else break;				
		
		// Fix this err case
		if (get_commod(commod_needed, &com_info) != 1) break;
		
		can_make = (((com_info.prodrate * agri_workers / no_of_reqs) / 500) * morale) * res_level;
		
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s  %s:",ECON_OK, commod_needed), 0);
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Need......: %f",ECON_OK, qty_needed), 0);
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Can gather: %f",ECON_OK, can_make), 0);
						
		if (can_make > qty_needed) { // Ah, planet is producing enough. Lets put the rest in store.			
			temp_d = can_make - qty_needed;
			
			health += health_inc;
			
			total_space = get_total_space(fd, tech_level);
					
			if (bext(market_data, tprintf("%s_%s",unparse_dbref(zone), T(food_dest)), 4, tmp) == 0) {
				do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. (usedspace) Exiting. ",ECON_ERR), 0);
				return 1;
			}
			usedspace = parse_double(tmp);	
			
			if (total_space > (usedspace + temp_d)) {		
				do_remit(1, unparse_dbref(monitor_room), tprintf("%s      We put the surplus of %f %s in %s",ECON_OK, temp_d, commod_needed, fd), 0);
				err = do_add_commod(zone, T(food_dest), T(commod_needed), unparse_number(temp_d));	
			} else {
				do_remit(1, unparse_dbref(monitor_room), tprintf("%s      We put the surplus of %f %s in %s, but only %f can fit.",ECON_OK, temp_d, commod_needed, fd, (total_space - usedspace)), 0);
				err = do_add_commod(zone, T(food_dest), T(commod_needed), unparse_number(total_space - usedspace));
			}		
			
			if (err != 0) {
				// make some errors
			}
		} else if (can_make < qty_needed) {
			// BUY
			// First we take away the ones we can produce...
			temp_d = qty_needed - can_make;
			// Oh, we gotta look in store...
			has_commod(zone, T(food_sour), T(commod_needed), tp);
			temp_d2 = parse_double(tmp);
			if (temp_d2 > 0) {
				// Oh, we have some in store!
				if (temp_d2 > temp_d) {
					// We even have more in store than we need...
					do_remit(1, unparse_dbref(monitor_room), tprintf("%s      We take the missing %f of %s from %s",ECON_OK, temp_d, commod_needed, T(fs)), 0);
					err = do_rem_commod(zone, T(fs), T(commod_needed), unparse_number(temp_d));
					// Make a error check here...
					temp_d = 0;
					health += health_inc;
				} else {
					// We have in store, but not enough...
					do_remit(1, unparse_dbref(monitor_room), tprintf("%s      We take %f of the missing %f of %s from %s",ECON_OK, temp_d2, temp_d, commod_needed, fs), 0);
					err = do_rem_commod(zone, fs, T(commod_needed), unparse_number(temp_d2));
					// Also, make a errors check here
					temp_d -= temp_d2;
				}
			}
			
			if (temp_d > 0) {
				// Mm, still some left. We just charge the owner for it.
				strcpy(tmp, unparse_number(com_info.cost));
				exch(local_currency, tp);
				tmpd = parse_double(tmp);
				
				health -= health_inc;
				
				do_remit(1, unparse_dbref(monitor_room), tprintf("%s      We didn't have enough %s in %s. Charge bank for the last %f.",ECON_OK, commod_needed, fd, temp_d), 0);
				err = uncrank_bank(zone, T("BAN_1"), &bankd);
							
				bankd.money -= (tmpd * temp_d);
				investment += (tmpd * temp_d);
				
				err = crank_bank(zone, T("BAN_1"), &bankd);								
				do_remit(1, unparse_dbref(monitor_room), tprintf("%s      The bill for %f %s came to %f.",ECON_OK, temp_d, commod_needed, (tmpd * temp_d)), 0);
				temp_d = 0;
			}			
		}				
	} while (rp != NULL);
	
	total_rent = (((rent * res_level) / 24) / 30) * (population - slaves);
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The rental income for %f people is %f",ECON_OK, (population - slaves), total_rent), 0);
	if (slaves > agri_workers) {
		total_agri_wages = 0;
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The wages for 0 agri workers is 0 (all are slaves)",ECON_OK), 0);
	} else {
		total_agri_wages = (agri_workers - slaves) * wage_agr;
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The wages for %f agri workers is %f (%f are slaves)",ECON_OK, agri_workers, total_agri_wages, slaves), 0);
	}
	
	
	err = uncrank_bank(zone, T("BAN_1"), &bankd);

	bankd.money += total_rent;
	life_income += total_rent;
	bankd.money -= total_agri_wages;
	investment += total_agri_wages;

	err = crank_bank(zone, T("BAN_1"), &bankd);
	
	if (health > 100) {health = 100;}
	if (health < 0) {health = 0;}
	strcpy(tmp, tprintf("%f",health));
	(void) atr_add(zone, "HEALTH", tmp, GOD, (AF_MDARK + AF_WIZARD + AF_NOPROG));	
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The health is set to %f.",ECON_OK, health), 0);
	
	strcpy(tmp, tprintf("%f",life_income));
	(void) atr_add(zone, "LIFE_INCOME", tmp, GOD, (AF_MDARK + AF_WIZARD + AF_NOPROG));
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The LIFE_INCOME is set to %f",ECON_OK, life_income), 0);
	
	strcpy(tmp, tprintf("%f",investment));	
	(void) atr_add(zone, "INVESTMENT", tmp, GOD, (AF_MDARK + AF_WIZARD + AF_NOPROG));
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s  The INVESTMENT is set to %f",ECON_OK, investment), 0);
	
	free(commod_needed);
	return 1;
	
}

int fac_trig(dbref player, dbref thing, char const *pattern __attribute__ ((__unused__)), ATTR *atr, void *args) {
	
	/*
	 * Function that does the actual work needed for factories. It's divided into several steps,
	 * look at the do_remit for further information.
	 *
	 */
	
	dbref zone, sobj;
	char building[10];
	char tmp[BUFFER_LEN];
	char *tmpp;
	char *tp;	
	char *tp3[15];
	char factory[17];
	char prod[25];
	char dest[25];
	char renter[10];
	char owner[10];
	struct commods_t commod;
	struct storage_t factorys;
	double usedspace, morale, qty, tmpd, rent, tax, taxes;
	int techlevel;
	
	int err,i,i2,found; // Used as an error indicator.
	
	tmpp = &tmp[0];
	
	zone = 0;	
	strcpy(factory, AL_NAME(atr));
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("\n%s  Starting cycle on %s:",ECON_OK, factory), 0);
	// Ok, we need to split the attr name into zone and building number...
	err = split_building(factory, tmp, building);
	zone = parse_dbref(tmp);
		
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Is factory open?",ECON_OK), 0);	
	// Is building open?
	if (bext(factory_data, factory, 6, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s Failed to get attributes. Exiting. ",ECON_ERR), 0);
		return 1;
	}
	if (strncasecmp(tmp, "Open", 3) == 0) {	
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      YES",ECON_OK), 0);
	} else {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      NO",ECON_ERR), 0);
		return 1;
	} 
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Rent cycle...",ECON_OK), 0);
	// insert rent cycle here	
	if (bext(factory_data, factory, 1, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes (owner). Exiting. ",ECON_ERR), 0);
		return 1;
	}	
	strcpy(owner, tmp);
	if (bext(factory_data, factory, 2, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes (renter). Exiting. ",ECON_ERR), 0);
		return 1;
	}	
	strcpy(renter, tmp);
	if (bext(factory_data, factory, 11, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes (rent). Exiting. ",ECON_ERR), 0);
		return 1;
	}	
	rent = parse_double(tmp);
	
	
	// If renter = owner, do nothing.
	if (parse_dbref(owner) != parse_dbref(renter)) {		
		get_tax(zone, tmp);
		tax = parse_double(tmp);		
		taxes = rent * (tax * 0.01);
		(void) check_account(zone, T("BAN_1"), T(renter), tmp);
		tmpd = parse_double(tmp);
		
		if (tmpd < rent) { // Renter is broke. Call the hitmens...
			tp = (char *) alloca (BUFFER_LEN);
			strcpy(tp, unparse_dbref(econ_parent));
			strcat(tp, T("/TR_RENTER_BROKE"));			
		
			strcpy(tmp, unparse_dbref(zone));
			tp3[0] = &renter[0];
			tp3[1] = &renter[0];
			tp3[2] = &tmp[0];
			tp3[3] = &building[0];
			tp3[4] = 0;
			tp3[5] = 0;
			tp3[6] = 0;
			tp3[7] = 0;
			tp3[8] = 0;
			tp3[9] = 0;
			tp3[10] = 0;
			tp3[11] = 0;
			tp3[12] = 0;
			
			do_trigger(1, tp, tp3);
			
			snprintf(tp, 80, "%s      Renter %s (%s) is broke. Returning the building to owner.",ECON_ERR, Name(parse_dbref(renter)), renter);
			do_remit(1, unparse_dbref(monitor_room), tp, 0);
			do_reposess(zone, building);
			return 1;		 
		}
		
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Charging %s (%s) %f for rental.",ECON_OK, Name(parse_dbref(renter)), renter, rent), 0);
		(void) withdraw_account(zone, T("BAN_1"), T(renter), rent);
		
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Paying %f in tax for rental. Tax rate = %f",ECON_OK, taxes, tax), 0);
		add_taxes(zone, taxes);
		
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Depositing %f to %s (%s) account.",ECON_OK, (rent - taxes), Name(parse_dbref(owner)), owner), 0);
		(void) deposit_account(zone, T("BAN_1"), T(owner), (rent - taxes));		
	}
		
		
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Removing commods...",ECON_OK), 0);	
	// Does the factory has the needed commods? If yes, does also have space in dest? If not, does it have space in stock?
	// If yes, remove commods
	
	morale = get_morale(zone);	
	if (bext(factory_data, factory, 8, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. Exiting. ",ECON_ERR), 0);
		return 1;
	}
	if (get_commod(tmp, &commod) == 1) {
		strcpy(prod, tmp);
		if (bext(factory_data, factory, 7, tmp) == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. Exiting. ",ECON_ERR), 0);
			return 1;
		}		
		qty = parse_double(tmp) * (morale * 0.01); // How many will we produce when considering MORALE?	
				
		if (get_store_str(zone,	building, tmp) != 1) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Problem reading factory data. (z-f=%i-%s) Exiting. ",ECON_ERR, zone, building), 0);
			return 1;
		}
		uncrank(tmp, &factorys);		
		
		// Checking if we have the needed commods in storage. Otherwise we just exit clean.		
		for (i=0; i < commod.reqs; i++) {
			has_commod(zone, building, commod.req[i].name, tmpp);
			tmpd = parse_double(tmp);			
			if ((qty * commod.req[i].qty) > tmpd) {
				do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Not enough %s in stockpile (%f > %f). Exiting. ",ECON_ERR,commod.req[i].name, (qty * commod.req[i].qty), tmpd), 0);
				return 1;
			}
		}
		
		// Need to check the free storage on dest before we do anything at all...
		// Not made yet...
		if (bext(factory_data, factory, 5, tmp) == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. (destination) Exiting. ",ECON_ERR), 0);
			return 1;
		}
		strcpy(dest, tmp);
		sobj = 0;
		switch (dest[0]) {
			case 'F':
				sobj = factory_data;
				break;
			case 'W':
				sobj = ware_data;
				break;			      
			case 'M':
				switch (dest[1]) {
					case 'I':
						sobj = mine_data;
						break;
					case 'A':
						sobj = market_data;
						break;
				} break;
			default: return	0;
		}
		
		if (sobj == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get destination. Exiting. ",ECON_ERR), 0);
			return 1;
		}		
		if (bext(sobj, factory, 3, tmp) == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. (techlevel) Exiting. ",ECON_ERR), 0);
			return 1;
		}
		techlevel = parse_integer(tmp);	
		if (bext(sobj, factory, 4, tmp) == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. (usedspace) Exiting. ",ECON_ERR), 0);
			return 1;
		}
		usedspace = parse_double(tmp);	
		if (get_total_space(dest, techlevel) < (usedspace + qty + (qty * 0.02))) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Not enough room on destination. Exiting. ",ECON_ERR), 0);
			return 1;
		}				
		
		// Finally... Remove the commods and add produced into destination
		for (i=0; i < commod.reqs; i++) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Removing %f of %s from stockpile.",ECON_OK,(qty * commod.req[i].qty), commod.req[i].name), 0);
			do_rem_commod(zone, building, commod.req[i].name, tprintf("%f",(qty * commod.req[i].qty)));
		}
		
		// And then... We produce!		
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Produce commods...",ECON_OK), 0);
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Producing %f of %s...",ECON_OK, qty, prod), 0);
		do_add_commod(zone, dest, prod, tprintf("%f",qty));
		
		// Don't forget waste production... About 2% should be ok.
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Making %f of Waste...",ECON_OK, (qty*0.02)), 0);
		do_add_commod(zone, dest, T("Waste"), tprintf("%f",(qty*0.02)));
	}
		
		
	// yay!
		
	return 1;
}

int min_trig(dbref player, dbref thing, char const *pattern __attribute__ ((__unused__)), ATTR *atr, void *args) {
		
	/*
	 * Function that does the actual work needed for factories. It's divided into several steps,
	 * look at the do_remit for further information.
	 *
	 */
	
	dbref zone, sobj;
	char building[10];
	char tmp[BUFFER_LEN];
	char *tmpp;
	char *tp;	
	char *tp3[15];
	char mine[17];
	char prod[25];
	char dest[25];
	char renter[10];
	char owner[10];
	struct commods_t commod;
	struct storage_t factorys;
	double usedspace, morale, qty, tmpd, rent, tax, taxes;
	int techlevel;
	
	int err,i,i2,found; // Used as an error indicator.
	
	tmpp = &tmp[0];
	zone = 0;	
	strcpy(mine, AL_NAME(atr));	
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("\n%s  Starting cycle on %s:", ECON_OK, AL_NAME(atr)), 0);
	// Ok, we need to split the attr name into zone and building number...
	err = split_building(mine, tmp, building);
	zone = parse_dbref(tmp);

	do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Is mine open?",ECON_OK), 0);	
	// Is building open?
	if (bext(mine_data, mine, 6, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s Failed to get attributes. Exiting. ",ECON_ERR), 0);
		return 1;
	}
	if (strncasecmp(tmp, "Open", 3) == 0) {	
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      YES",ECON_OK), 0);
	} else {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      NO",ECON_ERR), 0);
		return 1;
	} 
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Rent cycle...",ECON_OK), 0);
	// insert rent cycle here	
	if (bext(mine_data, mine, 1, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes (owner). Exiting. ",ECON_ERR), 0);
		return 1;
	}	
	strcpy(owner, tmp);
	if (bext(mine_data, mine, 2, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes (renter). Exiting. ",ECON_ERR), 0);
		return 1;
	}	
	strcpy(renter, tmp);
	if (bext(mine_data, mine, 11, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes (rent). Exiting. ",ECON_ERR), 0);
		return 1;
	}	
	rent = parse_double(tmp);
		
	// If renter = owner, do nothing.
	if (parse_dbref(owner) != parse_dbref(renter)) {		
		get_tax(zone, tmp);
		tax = parse_double(tmp);		
		taxes = rent * (tax * 0.01);
		(void) check_account(zone, T("BAN_1"), T(renter), tmp);
		tmpd = parse_double(tmp);
		
		if (tmpd < rent) { // Renter is broke. Call the hitmens...
			tp = (char *) alloca (BUFFER_LEN);
			strcpy(tp, unparse_dbref(econ_parent));
			strcat(tp, T("/TR_RENTER_BROKE"));			
		
			strcpy(tmp, unparse_dbref(zone));
			tp3[0] = &renter[0];
			tp3[1] = &renter[0];
			tp3[2] = &tmp[0];
			tp3[3] = &building[0];
			tp3[4] = 0;
			tp3[5] = 0;
			tp3[6] = 0;
			tp3[7] = 0;
			tp3[8] = 0;
			tp3[9] = 0;
			tp3[10] = 0;
			tp3[11] = 0;
			tp3[12] = 0;
			
			do_trigger(1, tp, tp3);
			
			snprintf(tp, 80, "%s      Renter %s (%s) is broke. Returning the building to owner.",ECON_ERR, Name(parse_dbref(renter)), renter);
			do_remit(1, unparse_dbref(monitor_room), tp, 0);
			do_reposess(zone, building);
			return 1;		 
		}
		
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Charging %s (%s) %f for rental.",ECON_OK, Name(parse_dbref(renter)), renter, rent), 0);
		(void) withdraw_account(zone, T("BAN_1"), T(renter), rent);
		
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Paying %f in tax for rental. Tax rate = %f",ECON_OK, taxes, tax), 0);
		add_taxes(zone, taxes);
		
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Depositing %f to %s (%s) account.",ECON_OK, (rent - taxes), Name(parse_dbref(owner)), owner), 0);
		(void) deposit_account(zone, T("BAN_1"), T(owner), (rent - taxes));		
	}	
		
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Mine commods...", ECON_OK), 0);
	
	morale = get_morale(zone);	
	if (bext(mine_data, mine, 8, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. Exiting. ",ECON_ERR), 0);
		return 1;
	}
	if (get_commod(tmp, &commod) == 1) {
		strcpy(prod, tmp);
		if (bext(mine_data, mine, 7, tmp) == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. Exiting. ",ECON_ERR), 0);
			return 1;
		}		
		qty = parse_double(tmp) * (morale * 0.01); // How many will we produce when considering MORALE?	
				
		if (get_store_str(zone,	building, tmp) != 1) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Problem reading mine data. (z-f=%i-%s) Exiting. ",ECON_ERR, zone, building), 0);
			return 1;
		}
		uncrank(tmp, &factorys);
		
		if (strcasecmp(prod, "Waste") == 0) { // Waste... :)
			// Checking if we have the needed commods in storage. Otherwise we just exit clean.		
			has_commod(zone, building, T("Waste"), tmpp);
			tmpd = parse_double(tmp);			
			if ((qty) > tmpd) {
				do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Not enough %s in stockpile (%f > %f). Exiting. ",ECON_ERR,T("Waste"), qty, tmpd), 0);
				return 1;
			}			
		}
		
		// Need to check the free storage on dest before we do anything at all...
		// Not made yet...
		if (bext(mine_data, mine, 5, tmp) == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. (destination) Exiting. ",ECON_ERR), 0);
			return 1;
		}
		strcpy(dest, tmp);
		sobj = 0;
		switch (dest[0]) {
			case 'F':
				sobj = factory_data;
				break;
			case 'W':
				sobj = ware_data;
				break;			      
			case 'M':
				switch (dest[1]) {
					case 'I':
						sobj = mine_data;
						break;
					case 'A':
						sobj = market_data;
						break;
				} break;
			default: return	0;
		}
		
		if (sobj == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get destination. Exiting. ",ECON_ERR), 0);
			return 1;
		}		
		if (bext(sobj, tprintf("%s_%s",unparse_dbref(zone),dest), 3, tmp) == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. (techlevel) Exiting. ",ECON_ERR), 0);
			return 1;
		}
		techlevel = parse_integer(tmp);	
		if (bext(sobj, tprintf("%s_%s",unparse_dbref(zone),dest), 4, tmp) == 0) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Failed to get attributes. (usedspace) Exiting. ",ECON_ERR), 0);
			return 1;
		}
		usedspace = parse_double(tmp);	
		if (get_total_space(dest, techlevel) < (usedspace + qty + (qty * 0.02))) {
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s    Not enough room on destination. Exiting. ",ECON_ERR), 0);
			return 1;
		}				
		
		if (strcasecmp(prod, "Waste") == 0) { // Waste... :)
			// Finally... Remove the commods and add produced into destination
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Removing %f of %s from stockpile.",ECON_OK,qty, T("Waste")), 0);
			do_rem_commod(zone, building, T("Waste"), tprintf("%f",qty));
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Producing %f of %s...",ECON_OK, (qty*2), T("Oil")), 0);
			do_add_commod(zone, dest, T("Oil"), tprintf("%f",(qty * 2)));			
		} else {			
			// And then... We produce!		
			do_remit(1, unparse_dbref(monitor_room), tprintf("%s      Producing %f of %s...",ECON_OK, qty, prod), 0);
			do_add_commod(zone, dest, prod, tprintf("%f",qty));
		}
	}
		
		
	// yay!		
	return 1;
}

int do_ftrigger(dbref enactor) {
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("\nFactory cycle started..."), 0);
	(void) atr_iter_get(GOD, factory_data, "#*_FAC_*", 0, fac_trig, NULL);
	
	return 1;
}

int do_mtrigger(dbref enactor) {
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("\nMine cycle started..."), 0);
	(void) atr_iter_get(GOD, mine_data, "#*_MIN_*", 0, min_trig, NULL);
	
	return 1;
}
/*
   int tr_wages(dbref player, dbref thing, char const *pattern __attribute__ ((__unused__)), ATTR *atr, void *args) {
  	char tmp[BUFFER_LEN];
  	char *tp = tmp;
  	char building[20];
  	char renter[20];
  	int found;
  	double wages, workers, wage;
	struct storage_t *storages;  
	int i;	
  
	found =	0;
  
	storages = (struct storage_t *) alloca (sizeof(struct storage_t));
	
	strcpy(tmp, uncompress(atr->value));
	strcpy(building, AL_NAME(atr));	
	
	do_remit(1, unparse_dbref(monitor_room), tprintf("%s   Starting to collect wages at %s ",ECON_OK, building), 0);
	
	if (bext(thing, building, 9, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s     Failed to get workers. Exiting. ",ECON_ERR), 0);
		return 1;
	}
	workers = parse_double(tmp);
	if (bext(thing, building, 9, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s     Failed to get wages. Exiting. ",ECON_ERR), 0);
		return 1;
	}
	wage = parse_double(tmp);
	if (bext(thing, building, 2, tmp) == 0) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s     Failed to get renter. Exiting. ",ECON_ERR), 0);
		return 1;
	}
	strcpy(renter, tmp);

	wages = workers * wage;
	
	(void) check_account(zone, T("BAN_1"), &renter, tmp);
	
	if (parse_double(tmp) > wages) {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s     Withdrawing %f from %s (%s) account for wages.",ECON_ERR), 0);
		(void) withdraw_account(zone, T("BAN_1"), &renter, wages);		
	} else {
		do_remit(1, unparse_dbref(monitor_room), tprintf("%s     Failed to get renter. Exiting. ",ECON_ERR), 0);
	}
	
	return 1;
}*/
